package com.zhf.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.zhf.base.BaseServlet;
import com.zhf.pojo.Course;
import com.zhf.pojo.Course_Lesson;
import com.zhf.pojo.Course_Section;
import com.zhf.service.CourseContentService;
import com.zhf.service.impl.CourseContentServiceImpl;
import com.zhf.utils.DateUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/31 16:14
 */
@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {

    /**
     * 展示对应课程的章节与课时信息
     *
     * @param request
     * @param response
     */
    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取参数
            String course_id = request.getParameter("course_id");

            //业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            List<Course_Section> list = contentService.findSectionAndLessonByCourseId(Integer.parseInt(course_id));

            //处理结果
            String string = JSON.toJSONString(list);

            //返回结果
            response.getWriter().print(string);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据课程id回显课程信息
     *
     * @param request
     * @param response
     */
    public void findCourseById(HttpServletRequest request, HttpServletResponse response) {
        try {
            String courseId = request.getParameter("course_id");
            CourseContentService contentService = new CourseContentServiceImpl();
            Course course = contentService.findCourseById(Integer.parseInt(courseId));

            //处理结果
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name");

            String result = JSON.toJSONString(course, filter);

            //响应请求
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存&修改章节信息
     *
     * @param request
     * @param response
     */
    public void saveOrUpdateSection(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取参数
            Map<String, Object> map = (Map) request.getAttribute("map");

            //创建一个章节对象Course_Section
            Course_Section section = new Course_Section();

            //使用BeanUtils工具类将map中的数据封装到section对象中
//            BeanUtils.populate(section, map);
            BeanUtils.copyProperties(section, map.get("section"));

            //业务处理
            CourseContentService contentService = new CourseContentServiceImpl();

            if (section.getId() == 0) {
                String result = contentService.saveSection(section);
                //响应结果
                response.getWriter().print(result);
            } else {
                String result = contentService.updateSection(section);
                response.getWriter().print(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateSectionStatus(HttpServletRequest request, HttpServletResponse response) {
        try {
            String id = request.getParameter("id");
            String status = request.getParameter("status");
            CourseContentService contentService = new CourseContentServiceImpl();
            String s = contentService.updateSectionStatus(Integer.parseInt(id), Integer.parseInt(status));
            response.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void saveOrUpdateLesson(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取参数
            Map<String, Object> map = (Map) request.getAttribute("map");

            //创建一个课时对象
            Course_Lesson lesson = new Course_Lesson();

            //使用BeanUtils工具类将map中的数据封装到lesson对象中
            BeanUtils.populate(lesson, map);
            //业务处理
            CourseContentService contentService = new CourseContentServiceImpl();

            if (lesson.getId() == 0) {
                lesson.setCreate_time(DateUtils.getDateFormart());
                lesson.setUpdate_time(DateUtils.getDateFormart());
                String result = contentService.saveLesson(lesson);
                //响应结果
                response.getWriter().print(result);
            } else {
                lesson.setUpdate_time(DateUtils.getDateFormart());
                String result = contentService.updateLesson(lesson);
                response.getWriter().print(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
